package com.ygj.inspection.service.impl;

import com.base.common.ConfigConstants;
import com.base.common.entity.InspectionTicket;
import com.base.common.enums.InspectionTicketCategories;
import com.base.common.json.JsonUtil;
import com.base.common.persistent.dao.*;
import com.base.common.persistent.model.InspectionCategories;
import com.base.common.persistent.model.InspectionPointsProject;
import com.base.common.persistent.model.InspectionProject;
import com.base.common.persistent.model.InspectionTicketDetails;
import com.ygj.inspection.service.InspectionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Service("inspectionService")
public class InspectionServiceImpl implements InspectionService {
	
	@Resource
	private EmployeeCompoundsMapper employeeCompoundsMapper;
	
	@Resource
	private InspectionCategoriesMapper inspectionCategoriesMapper;
	
	@Resource
	private InspectionProjectMapper inspectionProjectMapper;

	@Resource
	private InspectionTicketDetailsMapper inspectionTicketDetailsMapper;
	
	@Resource
	private InspectionTicketMapper inspectionTicketMapper;
	
	@Override
	public List<InspectionCategories> getAllInspectionCategories() {
		return inspectionCategoriesMapper.getAllInspectionCategories();
	}

	/**
	 * 生成巡查工单工单号
	 * 
	 * @param tickets
	 * @return
	 */
	@Transactional
	private String getCode(String prefixCode) {
		String lastedCode = inspectionTicketMapper.queryLastCode();
		DecimalFormat df = new DecimalFormat(ConfigConstants.TICKET_CODE_NUMS);
		String code = null;
		if(lastedCode != null && lastedCode.trim() != ""){
			code = df.format(Integer.parseInt(lastedCode.substring(1).trim()) + 1);
		} else {
			code = df.format(1);
		}
		code = prefixCode + code;
		return code;
	}
	
	@Override
	public List<InspectionProject> getInspectionProject(
			int inspectionCategories, String inspectionPointCode) {
		return inspectionProjectMapper.getInspectionProjectByCompoundsIdAndInspectionCategoriesAndInspectionPointCode(inspectionCategories, inspectionPointCode);
	}

	@Transactional
	@Override
	// 经纬度，员工小区中间id
	public int subInspectionTicket(int employeeId,
			List<String> inspectionProjectsStatusList, String coordinates, int compoundsId) {
		int result = 0;
		// 添加巡查工单
		String code = getCode(InspectionTicketCategories.INSPECTION.getName());	// 生成工单code
		com.base.common.persistent.model.InspectionTicket inspectionTicket = new com.base.common.persistent.model.InspectionTicket();
		inspectionTicket.setInspectionTime(new Date());
		inspectionTicket.setEmployeeCompoundsId(employeeCompoundsMapper.queryEmployeeCompoundsIdByEmployeeIdAndCompoundsId(employeeId, compoundsId));
		inspectionTicket.setCode(code);
		inspectionTicket.setCoordinates(coordinates);
		result = inspectionTicketMapper.addInspectionTicket(inspectionTicket);
		
		// 添加巡查工单详细
		InspectionTicketDetails inspectionTicketDetails = new InspectionTicketDetails();
		inspectionTicketDetails.setInspectionTicket(inspectionTicketMapper.queryInspectionTicketByCode(code));
		for(String inspectionProjectsStatus : inspectionProjectsStatusList) {
			inspectionTicketDetails.setStatus(JsonUtil.string2Obejct(inspectionProjectsStatus, InspectionTicket.class).getStatus());
			InspectionPointsProject inspectionPointsProject = new InspectionPointsProject();
			inspectionPointsProject.setId(JsonUtil.string2Obejct(inspectionProjectsStatus, InspectionTicket.class).getInspectionPointsProjectId());
			inspectionTicketDetails.setInspectionPointsProject(inspectionPointsProject);
			inspectionTicketDetailsMapper.addInspectionTicketDetails(inspectionTicketDetails);
		}
		
		return result;
	}


}
